//
//  XMHNetWorkongMethods.m
//  B段项目
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 徐茂怀. All rights reserved.
//

#import "XMHNetWorkongMethods.h"

@implementation XMHNetWorkongMethods
+(void)getDataVyString:(NSString *)string BodyString:(NSDictionary *)bodyDic WithDataBlock:(void(^)(id data))dataBlock WithFailure:(void (^)(NSError *))errorBlock
{
    //1.字符串的转码
    string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:string]];
    //创建管理者对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置超时时间
    manager.requestSerializer.timeoutInterval = 7;
    //设置允许请求的类别
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js",@"application/x-javascript", nil];
    //4.开始请求
    if (!bodyDic) {
        //如果是bodyDic为空就执行get请求
        [manager GET:string parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            errorBlock(error);
        }];
    }
    else
    {
        [manager POST:string parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            errorBlock(error);
        }];
    }
    
}
+(void)getDataVyString:(NSString *)string BodyString:(NSDictionary *)bodyDic WithDataBlock:(void(^)(id data))dataBlock
{
    //1.字符串的转码
    string = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:string]];
    //创建管理者对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //设置允许请求的类别
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", @"application/javascript", @"text/js",@"application/x-javascript", nil];
    //4.开始请求
    if (!bodyDic) {
        //如果是bodyDic为空就执行get请求
        [manager GET:string parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
    }
    else
    {
        [manager POST:string parameters:bodyDic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
            dataBlock(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        }];
    }
    
}

@end
